from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from config import PWD_CONTENT


Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    password = Column(String)

    @staticmethod
    def get_password_hash(password: str) -> str:
        """
        对密码进行哈希处理
        :param password: 原始密码
        :return: 哈希后的密码
        """
        return PWD_CONTENT.hash(password)

    @classmethod
    def create(self, cls, username: str, password: str):
        """
        创建新用户，自动处理密码哈希
        """
        return cls(
            username=username,
            password=self.get_password_hash(password)
        )

    def set_password(self, password: str):
        """
        更新用户密码
        """
        self.password = self.get_password_hash(password)